package com.twelve.all.utils;

import android.os.CountDownTimer;
import android.text.TextUtils;
import android.widget.TextView;

import java.text.SimpleDateFormat;

/**
 * Created by 张龙臣 on 2018/11/12.
 */

public class TimeDown extends CountDownTimer {

    private final TextView mTextView;
    private final String mEndText;

    /**
     * @param millisInFuture    The number of millis in the future from the call  剩余时间
     *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
     *                          is called.
     * @param countDownInterval The interval along the way to receive  间隔
     *                          {@link #onTick(long)} callbacks.
     */

    public TimeDown(long millisInFuture, long countDownInterval, TextView textView, String endText) {
        super(millisInFuture, countDownInterval);
        mTextView = textView;
        mEndText = endText;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("mm:ss");
        mTextView.setText(dateFormat.format(millisUntilFinished));
    }

    @Override
    public void onFinish() {
        mTextView.setEnabled(true);
        if (!TextUtils.isEmpty(mEndText))
            mTextView.setText(mEndText);
        mTextView.setClickable(true);
    }
}
